package org.czl.leetcode2;

public class l2207 {
    public long maximumSubsequenceCount(String text, String pattern){
        int fro = 0;
        int end = 0;
        int n = text.length();
        if(pattern.charAt(0) == pattern.charAt(1)){
            int k = 0;
            for(int i = 0; i < n; i++)if(text.charAt(i) == pattern.charAt(1))k++;
            return (long)(k + 1) * k /2;
        }
        long ans = 0;
        for(int i = 0; i < n; i++){
            int ret = text.charAt(i);
            if(ret == pattern.charAt(0)){
                fro ++;
            } else if(ret == pattern.charAt(1)){
                ans += fro;
                end++;
            }
        }
        ans += Math.max(end, fro);
        return ans;
    }
}
